package com.wjf.shortlink.shortlink.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wjf.shortlink.shortlink.dao.entity.LinkNetworkStatsDAO;
import com.wjf.shortlink.shortlink.dto.req.ShortLinkGroupStatsReqDTO;
import com.wjf.shortlink.shortlink.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

//短链接访问网络持久层
public interface LinkNetworkStatsMapper extends BaseMapper<LinkNetworkStatsDAO> {

    //获取短链接指定日期内访问网络统计
    @Select("SELECT " +
            "      tlns.network, " +
            "      SUM(tlns.cnt) AS cnt," +
            "FROM " +
            "    link tl INNER JOIN " +
            "    link_network_stats tlns ON tl.full_short_url = tlns.full_short_url " +
            "WHERE " +
            "     tlns.full_short_url = #{param.fullShortUrl}" +
            "     AND tl.gid = #{param.gid} " +
            "     AND tl.del_flag = '0' " +
            "     AND tl.enable_status = #{param.enableStatus} " +
            "     AND tlns.date BETWEEN #{param.startDate} AND #{param.endDate} " +
            "GROUP BY " +
            "     tlns.full_short_url,tl.gid,tlns.network;"
    )
    List<LinkNetworkStatsDAO> listNetworkStatsByShortLink(@Param("param") ShortLinkStatsReqDTO shortLinkStatsReqDTO);

    //根据分组获取指定日期内网络监控数据
    @Select("SELECT " +
            "     tlns.network, " +
            "     SUM(tlns.cnt) AS count " +
            "FROM " +
            "    link tl INNER JOIN " +
            "    link_network_stats tlns ON tl.full_short_url = tlns.full_short_url " +
            "WHERE " +
            "    tl.gid = #{param.gid} " +
            "    AND tl.del_flag = '0' " +
            "    AND tl.enable_status = '0' " +
            "    AND tlns.date BETWEEN #{param.startDate} AND #{param.endDate} " +
            "GROUP BY " +
            "     tl.gid, tlns.network;"
    )
    List<LinkNetworkStatsDAO> listNetworkStatsByGroup(@Param("param") ShortLinkGroupStatsReqDTO shortLinkGroupStatsReqDTO);
}
